package MineGod;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.geom.Rectangle;

public class EntityLavaWall extends Entity{
	private double xSpeed;
	private int currChunkIndex;
	private int chunkCount;
	private double delayStart;
	
	public EntityLavaWall(){
		x = 0.0;
		xSpeed = .1/400;
		currChunkIndex = 0;
		chunkCount = 1;
		delayStart = 0;
		mask = new Rectangle((float)(x), (float)(0), (float)Block.WIDTH, (float)BlockSand.WIDTH*Chunk.CHUNK_HEIGHT);
	}
	
	@Override
	public void interactWithPlayerOnCollision(Player player) {
		player.damage(15, DamageType.lava);
	}

	@Override
	public void doLogic(int delta, Chunk prevChunk, Chunk currChunk,
			Chunk nextChunk) {
		if (delayStart<400){
			delayStart++;
			xSpeed = .1/400*(delayStart);
			if (delayStart == 400)
			{
				xSpeed = .1;
			}
		}
		x = x+delta*xSpeed;
		int chunkIndex = (int)(x/Chunk.CHUNK_PIXEL_WIDTH);
		
		if (chunkIndex>currChunkIndex){
			chunkCount++;
			currChunkIndex = chunkIndex;
			if (chunkCount%5 == 0){
				xSpeed += .01;
				xSpeed = Math.min(xSpeed, .14);
			}
		}
		
		for (int y =0; y<Chunk.CHUNK_HEIGHT; y++){
			Utils.changeCurrentBlock(new BlockLava(), x, y*Block.WIDTH);
		}
		mask = new Rectangle((float)(x), (float)(0), (float)Block.WIDTH, (float)BlockSand.WIDTH*Chunk.CHUNK_HEIGHT);
	}

	@Override
	public void render(double worldPos) {
		if (!(x+Block.WIDTH-worldPos<0 || x-Block.WIDTH-worldPos>Chunk.CHUNK_WIDTH*Block.WIDTH)){
			for (int y =0; y<Chunk.CHUNK_HEIGHT; y++){
				GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
				GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
				BlockLava.lavaTex.bind();
				double renderX = x - worldPos;
				GL11.glColor3d(1.0,1.0,1.0); // Could make this faster!!
				GL11.glBegin(GL11.GL_QUADS);
					GL11.glNormal3d(0, 0, 1);
					GL11.glTexCoord2d(0.0, 0.0);
					GL11.glVertex3d(renderX, y*Block.WIDTH, zIndex);
					GL11.glTexCoord2d(1.0, 0.0);
					GL11.glVertex3d(renderX + Block.WIDTH, y*Block.WIDTH, zIndex);
					GL11.glTexCoord2d(1.0, 1.0);
					GL11.glVertex3d(renderX + Block.WIDTH, y*Block.WIDTH + Block.WIDTH, zIndex);
					GL11.glTexCoord2d(0.0, 1.0);
					GL11.glVertex3d(renderX, y*Block.WIDTH + Block.WIDTH, zIndex);
				GL11.glEnd();
			}
		}
	}
	
}
